package axdoc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import axdoc.model.CommonActivity;

@Repository
public class CommonActivityDao {

	@Autowired
	JdbcTemplate jdbcTemplate;

	public int adddrugs(String drugs) {
		String sql = "insert into drugs(msg) values(?)";
		return jdbcTemplate.update(sql, new Object[] { drugs });
	}

	public int addAct(CommonActivity act) {
		String sql = "insert into table_common_activity(name,business_type,event_type,status,isdelete,sortorder,ct)"
				+ " values(?,?,?,?,?,?,now())";
		return jdbcTemplate.update(sql, new Object[] { act.getName(), act.getBusinessType(), act.getEventType(),
				act.getStatus(), act.getIsdelete(), act.getSortorder() });
	}

	public List<CommonActivity> query() {
		String sql = "select * from table_common_activity ";
		return jdbcTemplate.query(sql, new CommonActivityMapper());
	}

	public List<CommonActivity> queryByEventType(int eventType) {
		String sql = "select * from table_common_activity where event_type=?  ";
		return jdbcTemplate.query(sql, new Object[] { eventType }, new CommonActivityMapper());
	}

	public int addActRules(String rules, int actId) {
		String sql = "update table_common_activity set rules=? where id=? ";
		return jdbcTemplate.update(sql, new Object[] { rules, actId });
	}

	public int addActDones(String dones, int actId) {
		String sql = "update table_common_activity set dones=? where id=? ";
		return jdbcTemplate.update(sql, new Object[] { dones, actId });
	}

	public CommonActivity queryActivityById(int id) {
		String sql = "select * from table_common_activity where id=? ";
		return jdbcTemplate.queryForObject(sql, new Object[] { id }, CommonActivity.class);
	}
}

class CommonActivityMapper implements RowMapper<CommonActivity> {

	@Override
	public CommonActivity mapRow(ResultSet rs, int rowNum) throws SQLException {

		CommonActivity ca = new CommonActivity();
		ca.setId(rs.getInt("id"));
		ca.setName(rs.getString("name"));
		ca.setBusinessType(rs.getInt("business_type"));
		ca.setEventType(rs.getInt("event_type"));
		ca.setStatus(rs.getInt("status"));
		ca.setRules(rs.getString("rules"));
		ca.setDones(rs.getString("dones"));

		return ca;
	}

}